/**
 * 对象操作工具类
 * @version 1.0.0
 */
class ObjectUtils {

    /**
     * 统计对象转为数组
     * @param {Object} data 统计对象。示例：{"01":5, "05":2, ... "35":1}
     * @param {Boolean} withCount 返回值是否包含次数信息，默认 false
     * @param {Object} option 配置。示例：{op:'>', value:5}
     * @returns 返回开奖号码
     */
    statObjectToArr = (data, withCount = false, option = null) => {
        if (option) {
            data = this.filterObjectValues(data, option.op, option.value);
        }

        let list = [];
        for (let n in data) {
            list.push(withCount ? `${n}/${data[n]}` : n);
        }
        return list;
    }

    sortObjectKeys = (objData, isReverse = false) => {
        let keys = Object.keys(objData);
        keys.sort();
        if (isReverse) {
            keys.reverse();
        }
        let objSorted = {};
        for (let k of keys) {
            objSorted[k] = objData[k]
        }
        return objSorted;
        // return Object.keys(objData)
        //     .sort()
        //     .reduce((objSorted, key) => {
        //         objSorted[key] = objData[key];
        //         return objSorted;
        //     }, {});
    }
    sortObjectValues = (objData, isReverse = false) => {
        let keys = Object.keys(objData).sort(function (a, b) {
            return isReverse ? (objData[a] - objData[b]) : (objData[b] - objData[a]);
        });
        let objSorted = {};
        for (let key of keys) {
            objSorted[key] = obj[key];
        }
        return objSorted;
    }

    filterObjectValues = (objData, op = '>', value = 0) => {
        let objTemp = {};
        for (let k in objData) {
            switch (op) {
                case '>':
                    if (objData[k] > value) {
                        objTemp[k] = objData[k];
                    }
                    break;
                case '>=':
                    if (objData[k] >= value) {
                        objTemp[k] = objData[k];
                    }
                    break;
                case '<':
                    if (objData[k] < value) {
                        objTemp[k] = objData[k];
                    }
                    break;
                case '<=':
                    if (objData[k] <= value) {
                        objTemp[k] = objData[k];
                    }
                    break;
                default:
                    objTemp[k] = objData[k];
                    break;
            }
        }
        return objTemp;
    }
}

export default new ObjectUtils();